package javaweb;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

public class Addr extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        //获取客户端的IP地址
        String remoteAddr = request.getRemoteAddr();
        out.print(remoteAddr + "<br>");

        String realname = request.getParameter("realname");
        System.out.println("realname = " + realname);

        // 这个方法使用的比较多（可以动态获取应用的根路径）
        String contextPath = request.getContextPath();
        System.out.println("应用的根路径：" + contextPath);//  /javaweb

        //String realPath = request.getRealPath("/addr");
        //System.out.println("应用的全路径：" + realPath);

        //获取请求方式
        String method = request.getMethod();
        System.out.println("请求方式：" + method);

        //获取请求的URI
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);//   /javaweb/addr

        //获取servlet路径
        String servletPath = request.getServletPath();
        System.out.println(servletPath);// /addr
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        //获取客户端的IP地址
        String remoteAddr = request.getRemoteAddr();
        out.print(remoteAddr + "<br>");

        String username = request.getParameter("username");
        System.out.println(username);

        out.close();
    }
}
